package edu.lambdateacher.awt;

/*
LambdaTeacher
Copyright (C) 2000  Kim Mason
Copyright (C) 2014  Vitalij Zadneprovskij

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import edu.lambdateacher.PropertiesManager;
import edu.lambdateacher.expressions.Expression;

public class ApplicativeBtnActionListener implements ActionListener {

	private AWTUserInterface awtUserInterface;

	public ApplicativeBtnActionListener(AWTUserInterface i) {
		awtUserInterface = i;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (awtUserInterface.getTheexpr() != null) {
			boolean reducable = awtUserInterface.getTheexpr().containsReducable();
			Expression toreduce = awtUserInterface.getTheexpr().findApplicativeReducable();
			if (PropertiesManager.expdlogon == true) {
				String explain = "";
				if (reducable) {
					// explain normal reduction
					explain = explain
							+ "You have chosen to applicative reduce the current expression one step:\n\n";
					explain = explain + awtUserInterface.getTheexpr().stringRepresentation(false) + "\n\n";
					explain = explain
							+ "This will attempt to reduce the outermost expression, except for applications.  ";
					explain = explain
							+ "When an application is encountered, it will attempt to applicatively reduce the function, ";
					explain = explain + "and then the argument, before ";
					explain = explain
							+ "performing the substitution.  The current applicative reducable expression is:\n\n";
					explain = explain + toreduce.stringRepresentation(false) + "\n\n";
					explain = explain
							+ "The above expression will be reduced.\n";
					explain = explain
							+ "If it is possible to reduce an expression to normal form, normal reduction will ";
					explain = explain
							+ "always achieve normal form, but applicative reduction may not.";

				} else {
					explain = explain
							+ "The current expression can't be reduced, as it is already in normal form.";
				}

				MessageDialog mdlog = new MessageDialog(awtUserInterface,
						"Applicative Reduce", explain);
				mdlog.setVisible(true);
			}
			if (reducable)
				awtUserInterface.setTheexpr(awtUserInterface.getTheexpr().reduceExpression(toreduce));
		}
		String lambdastring = null;
		if (awtUserInterface.getTheexpr() != null) {
			lambdastring = awtUserInterface.getTheexpr().stringRepresentation(false);
		}
		if (lambdastring != null) {
			awtUserInterface.exprdisplay.setText(lambdastring);
		} else {
			awtUserInterface.exprdisplay.setText("");
		}
	}

}
